﻿//
//  WeatherView.cpp
//  Weather
//
//  Created by 朱建 31990839@qq.com on 16-08-18.
//  Copyright (c) 2014 http://www.9miao.com All rights reserved.
//

#include "WeatherView.h"


CWeatherView::CWeatherView()
{
}

CWeatherView::~CWeatherView()
{

}

bool CWeatherView::init()
{
	if (!CAScrollView::init())
		return false;

	DRect winRect = RootWindow::getInstance()->getFrame();

	CAPullToRefreshView* pHeaderRefreshView = CAPullToRefreshView::create(CAPullToRefreshView::CAPullToRefreshTypeHeader);
	pHeaderRefreshView->setLabelColor(CAColor_white);

	this->setBackgroundImage(CAImage::create("r/bk.jpg"));
	this->setShowsHorizontalScrollIndicator(false);
	this->setBounceHorizontal(false);
	this->setHeaderRefreshView(pHeaderRefreshView);
	this->layoutPullToRefreshView();

	CAView* pSepView = CAView::createWithColor(CAColor_white);
	pSepView->setLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_H_C(3, 0.42f)));
	pSepView->setAlpha(0.6f);
	this->addSubview(pSepView);

	m_pCityLabel = CALabel::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(30, 64)));
	m_pCityLabel->setText(UTF8("-----"));
	m_pCityLabel->setFontSize(50);
	m_pCityLabel->setTextAlignment(CATextAlignmentCenter);
	m_pCityLabel->setColor(CAColor_white);
	this->addSubview(m_pCityLabel);

	m_pWDLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(0, winRect.size.width / 2 + 10), DVerticalLayout_H_C(180, 0.25f)));
	m_pWDLabel->setText("--");
	m_pWDLabel->setFontSize(160);
	m_pWDLabel->setFontName("Regular");
	m_pWDLabel->setTextAlignment(CATextAlignmentRight);
	m_pWDLabel->setColor(CAColor_white);
	this->addSubview(m_pWDLabel);

	CAImageView* dimage = CAImageView::createWithImage(CAImage::create("r/d.png"));
	dimage->setLayout(DLayout(DHorizontalLayout_L_W(winRect.size.width / 2 + 10, 32), DVerticalLayout_H_C(32, 0.21f)));
	this->addSubview(dimage);

	m_pWeatherLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(winRect.size.width / 2 + 10, 0), DVerticalLayout_H_C(180, 0.3f)));
	m_pWeatherLabel->setText(UTF8("----"));
	m_pWeatherLabel->setFontSize(32);
	m_pWeatherLabel->setColor(CAColor_white);
	this->addSubview(m_pWeatherLabel);

	CAImageView* sdimage = CAImageView::createWithImage(CAImage::create("r/sd.png"));
	sdimage->setLayout(DLayout(DHorizontalLayout_L_W(winRect.size.width / 2 + 10, 32), DVerticalLayout_H_C(32, 0.30f)));
	this->addSubview(sdimage);

	m_pSDLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(winRect.size.width / 2 + 50, 0), DVerticalLayout_H_C(38, 0.298f)));
	m_pSDLabel->setText("--");
	m_pSDLabel->setFontSize(32);
	m_pSDLabel->setColor(CAColor_white);
	this->addSubview(m_pSDLabel);

	CAImageView* fximage = CAImageView::createWithImage(CAImage::create("r/fx.png"));
	fximage->setLayout(DLayout(DHorizontalLayout_L_W(winRect.size.width / 2 + 105, 32), DVerticalLayout_H_C(32, 0.30f)));
	this->addSubview(fximage);

	m_pFLLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(winRect.size.width / 2 + 145, 0), DVerticalLayout_H_C(38, 0.298f)));
	m_pFLLabel->setText("--");
	m_pFLLabel->setFontSize(32);
	m_pFLLabel->setColor(CAColor_white);
	this->addSubview(m_pFLLabel);

	m_pTodayLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(30, 30), DVerticalLayout_H_C(38, 0.4f)));
	m_pTodayLabel->setText(UTF8("--------"));
	m_pTodayLabel->setFontSize(32);
	m_pTodayLabel->setBold(true);
	m_pTodayLabel->setColor(CAColor_white);
	this->addSubview(m_pTodayLabel);

	m_pTodayWDLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(30, 30), DVerticalLayout_H_C(38, 0.4f)));
	m_pTodayWDLabel->setText(UTF8("---  ---"));
	m_pTodayWDLabel->setFontSize(32);
	m_pTodayWDLabel->setBold(true);
	m_pTodayWDLabel->setTextAlignment(CATextAlignmentRight);
	m_pTodayWDLabel->setColor(CAColor_white);
	this->addSubview(m_pTodayWDLabel);

	float h = winRect.size.height * 0.42f;
	m_pWeatherDetailView = CAScrollView::createWithLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_B(h + 5, 0)));
	m_pWeatherDetailView->setShowsHorizontalScrollIndicator(false);
	m_pWeatherDetailView->setBounceHorizontal(false);
	m_pWeatherDetailView->setShowsVerticalScrollIndicator(false);
	m_pWeatherDetailView->setBackgroundColor(CAColor_clear);
	this->addSubview(m_pWeatherDetailView);

	addNewCastView(0);
	addNewCastView(1);
	addNewCastView(2);
	addNewCastView(3);

	addTodayLivesWeather();
	return true;
}

CWeatherView* CWeatherView::create()
{
	CWeatherView *weaView = new CWeatherView();
	if (weaView && weaView->init())
	{
		weaView->autorelease();
		return weaView;
	}
	CC_SAFE_DELETE(weaView);
	return NULL;
}

void CWeatherView::addNewCastView(int index)
{
	CALabel* dateLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(30, 30), DVerticalLayout_T_H(index*64 + 16, 38)));
	dateLabel->setText(UTF8("------"));
	dateLabel->setFontSize(32);
	dateLabel->setColor(CAColor_white);
	m_pWeatherDetailView->addSubview(dateLabel);

	CALabel* wdLabel = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(30, 30), DVerticalLayout_T_H(index * 64 + 16, 38)));
	wdLabel->setText(UTF8("--  --"));
	wdLabel->setFontSize(32);
	wdLabel->setTextAlignment(CATextAlignmentRight);
	wdLabel->setColor(CAColor_white);
	m_pWeatherDetailView->addSubview(wdLabel);

	CAImageView* dimage = CAImageView::createWithImage(CAImage::create(""));
	dimage->setLayout(DLayout(DHorizontalLayout_W_C(48, 0.5f), DVerticalLayout_T_H(index * 64 + 16, 48)));
	m_pWeatherDetailView->addSubview(dimage);

	m_iTodayWeatherHeight = (index+1) * 64 + 20;

	ForecastsData d;
	d.dateLabel = dateLabel;
	d.wdLabel = wdLabel;
	d.dImage = dimage;
	m_vForcastsLabels.push_back(d);
}


CALabel* CWeatherView::createLivesLabel(int h, const std::string& szName, const std::string& szValue)
{
	DRect winRect = RootWindow::getInstance()->getFrame();

	CALabel* label1 = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(10, winRect.size.width / 2 + 30), DVerticalLayout_T_H(m_iTodayWeatherHeight, 38)));
	label1->setText(szName);
	label1->setFontSize(32);
	label1->setTextAlignment(CATextAlignmentRight);
	label1->setColor(CAColor_white);
	m_pWeatherDetailView->addSubview(label1);

	CALabel* label2 = CALabel::createWithLayout(DLayout(DHorizontalLayout_L_R(winRect.size.width / 2, 10), DVerticalLayout_T_H(m_iTodayWeatherHeight, 38)));
	label2->setText(szValue);
	label2->setFontSize(32);
	label2->setTextAlignment(CATextAlignmentLeft);
	label2->setColor(CAColor_white);
	m_pWeatherDetailView->addSubview(label2);

	return label2;
}

std::string CWeatherView::getWeatherIconPath(const std::string& szWeather)
{
	if (szWeather == UTF8("晴天") || szWeather == UTF8("晴"))
	{
		return "r/qingtian.png";
	}
	if (szWeather == UTF8("多云"))
	{
		return "r/duoyun.png";
	}
	if (szWeather == UTF8("阴"))
	{
		return "r/yintian.png";
	}
	if (szWeather == UTF8("阵雨"))
	{
		return "r/zhenyu.png";
	}
	if (szWeather == UTF8("雷阵雨"))
	{
		return "r/leizhenyu.png";
	}
	if (szWeather == UTF8("雷阵雨并伴有冰雹"))
	{
		return "r/lzybbybb.png";
	}
	if (szWeather == UTF8("雨夹雪"))
	{
		return "r/yujiaxue.png";
	}
	if (szWeather == UTF8("小雨"))
	{
		return "r/xiaoyu.png";
	}
	if (szWeather == UTF8("中雨"))
	{
		return "r/zhongyu.png";
	}
	if (szWeather == UTF8("大雨"))
	{
		return "r/dayu.png";
	}
	if (szWeather == UTF8("暴雨"))
	{
		return "r/baoyu.png";
	}
	if (szWeather == UTF8("大暴雨"))
	{
		return "r/dabaoyu.png";
	}
	if (szWeather == UTF8("特大暴雨"))
	{
		return "r/tedabaoyu.png";
	}
	if (szWeather == UTF8("阵雪"))
	{
		return "r/zhenxue.png";
	}
	if (szWeather == UTF8("小雪"))
	{
		return "r/xiaoxue.png";
	}
	if (szWeather == UTF8("中雪"))
	{
		return "r/zhongxue.png";
	}
	if (szWeather == UTF8("大雪"))
	{
		return "r/daxue.png";
	}
	if (szWeather == UTF8("暴雪"))
	{
		return "r/baoxue.png";
	}
	CCLog("xxxxxxxxxx: %s", szWeather.c_str());
	return "";
}

void CWeatherView::addTodayLivesWeather()
{
	CAView* pSepView2 = CAView::createWithColor(CAColor_white);
	pSepView2->setLayout(DLayout(DHorizontalLayoutFill, DVerticalLayout_T_H(m_iTodayWeatherHeight, 2)));
	pSepView2->setAlpha(0.3f);
	m_pWeatherDetailView->addSubview(pSepView2);
	m_iTodayWeatherHeight += 30;

	DRect winRect = RootWindow::getInstance()->getFrame();
	
	m_pLivesTQLabel = createLivesLabel(m_iTodayWeatherHeight, UTF8("天气现象："), "--");
	m_iTodayWeatherHeight += 50;
	m_pLivesWDLabel = createLivesLabel(m_iTodayWeatherHeight, UTF8("实时气温："), "--");
	m_iTodayWeatherHeight += 50;
	m_pLivesFXLabel = createLivesLabel(m_iTodayWeatherHeight, UTF8("风向："), "--");
	m_iTodayWeatherHeight += 50;
	m_pLivesFLLabel = createLivesLabel(m_iTodayWeatherHeight, UTF8("风力："), "--");
	m_iTodayWeatherHeight += 50;
	m_pLivesSDLabel = createLivesLabel(m_iTodayWeatherHeight, UTF8("空气湿度："), "--");
	m_iTodayWeatherHeight += 50;
	m_pWeatherDetailView->setViewSize(DSize(winRect.size.width, m_iTodayWeatherHeight));
}

void CWeatherView::setWeatherDetailFirstResponder(bool v)
{
	if (v)
	{
		m_pWeatherDetailView->becomeFirstResponder();
	}
	else
	{
		m_pWeatherDetailView->resignFirstResponder();
	}
}

void CWeatherView::setDatas(const WeatherData& d)
{
	m_pCityLabel->setText(d.szCityName);
	m_pWDLabel->setText(d.szTemperature);
	m_pWeatherLabel->setText(d.szWeather);
	m_pSDLabel->setText(d.szHumidity);
	m_pFLLabel->setText(d.szWindpower);
	m_pTodayLabel->setText(d.szTodayDate);
	m_pTodayWDLabel->setText(d.szTodayTemperature);
	m_pLivesTQLabel->setText(d.szWeather);
	m_pLivesWDLabel->setText(d.szTemperature);
	m_pLivesFXLabel->setText(d.szWinddirection);
	m_pLivesFLLabel->setText(d.szWindpower);
	m_pLivesSDLabel->setText(d.szHumidity);

	assert(d.vDayWeathers.size() == m_vForcastsLabels.size());
	for (int i = 0; i < m_vForcastsLabels.size(); i++)
	{
		m_vForcastsLabels[i].dateLabel->setText(d.vDayWeathers[i].szWeek);
		m_vForcastsLabels[i].wdLabel->setText(d.vDayWeathers[i].szDayTemperature);
		m_vForcastsLabels[i].dImage->setImage(CAImage::create(getWeatherIconPath(d.vDayWeathers[i].szDayweather)));
	}
}